<!-- 软件更新信息 -->
<template>
  <div class="table">
    <div class="container">
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>
            <el-icon>
              <Search />
            </el-icon> 查询条件：
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-select v-model.trim="searchCondition.type" placeholder="平台类型" class="">
            <el-option v-for="item in state.selectPlatformType" :label="item.dictLabel" :value="item.dictValue"
              :key="item.id"></el-option>
          </el-select>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-input v-model.trim="searchCondition.version_code" placeholder="版本号"></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-col>
      </el-row>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>查询结果：</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="handle-box">
        <el-button type="primary" :icon="Plus" class="mr10" :disabled="disabledbtn" @click="editVisible = true">新增
        </el-button>
        <el-popconfirm placement="top" width="160" title="你确定要删除吗?" @confirm="batchDelete('deleteAppVersionInfo')">
          <template #reference>
            <el-button type="danger" :icon="Delete" class="mr10" :disabled="disabledbtn">批量删除
            </el-button>
          </template>
        </el-popconfirm>
      </div>
      <el-table :data="tableState.tableList" border class="table" ref="multipleTableRef" v-loading="tableLoading"
        :reserve-selection="true" @row-dblclick="rowDblclick">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="type" label="平台类型" align="center">
        </el-table-column>
        <el-table-column prop="version_code" label="版本号" align="center">
        </el-table-column>
        <el-table-column prop="descriptions" label="描述信息" align="center" :formatter="handleFormatterDescriptions">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" align="center" :formatter="handleFormatterAddTime">
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" text :icon="View" @click="handleViewEdit(scope.row, 'view')">
              查看
            </el-button>
            <el-button type="primary" size="small" text :icon="Edit" @click="handleViewEdit(scope.row, 'edit')">
              编辑
            </el-button>
            <div class="mlr10 display-inline-block">
              <el-popconfirm placement="top" width="160" title="你确定要删除吗?"
                @confirm="deleteInfo('deleteAppVersionInfo', [id])">
                <template #reference>
                  <el-button text :icon="Delete" size="small" class="red" @click="id = scope.row.id">删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @update:current-page="handleCurrentChange" @update:page-size="handleSizeChange"
          layout="total, sizes, prev, pager, next, jumper" :page-size="searchCondition.size"
          :current-page="searchCondition.current" :page-sizes="pageSizes" :total="total" :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>

    <!-- 新增编辑查看弹出框 -->
    <el-dialog :title="dialogTitle" v-model="editVisible" width="50%" :close-on-click-modal="closeOnClickModal"
      @closed="closedFormDialog" draggable>
      <el-form ref="versionFormRef" :model="versionForm" :rules="rules" v-loading="formLoading" label-width="150px">
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="平台类型：" prop="type">
              <el-select v-model.trim="versionForm.type" placeholder="平台类型" class="">
                <el-option v-for="item in state.selectPlatformType" :label="item.dictLabel" :value="item.dictValue"
                  :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="版本号：" prop="version_code">
              <el-input v-model.trim="versionForm.version_code" placeholder="版本号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-tooltip placement="top" content=" 格式：1.1.2 新版本号必须大于之前的版本号">
              <el-icon class="icon-wzg-config">
                <InfoFilled />
              </el-icon>
            </el-tooltip>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="20">
            <el-form-item label="描述：" prop="descriptions">
              <div class="w-container">
                <div class="w-content" v-for="(item, index) in versionForm.descriptions" :key="item.id">
                  <el-input v-model.trim="item.name">
                  </el-input>
                  <el-icon class="w-i-plus" v-if="index === versionForm.descriptions.length - 1" @click="handleAddDesc">
                    <CirclePlus />
                  </el-icon>
                  <!-- <i class="el-icon-circle-plus-outline w-i-plus" v-if="index === versionForm.descriptions.length - 1"
                    @click="handleAddDesc"></i> -->
                  <el-icon class="w-i-remove" @click="handleDeleteDesc(item.id)">
                    <Remove />
                  </el-icon>
                  <!-- <i class="el-icon-remove-outline w-i-remove" @click="handleDeleteDesc(item.id)"></i> -->
                </div>
              </div>
              <div v-if="versionForm.descriptions.length === 0">
                <el-icon class="w-i-plus" @click="handleAddDesc">
                  <CirclePlus />
                </el-icon>
                <!-- <i class="el-icon-circle-plus-outline w-i-plus" @click="handleAddDesc"></i> -->
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="disabledbtn" v-if="!isDisabledbtn"
            @click="submitForm('commonAppVersionInfoListInfo', getTableInfo, handleValidAssignment)">确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { common as Common } from '@/utils/common.js';
import { View, Delete, Edit, Search, Plus, Refresh, CirclePlus, Remove } from '@element-plus/icons-vue';
import { api as Request } from '@/utils/api';
import { useTable } from '@/hooks/useTable.js';
import { useDialogForm } from '@/hooks/useDialogForm.js';
import { useDictData } from '@/hooks/network/useDictData.js';
import useRules from '@/hooks/useRules.js';
const rules = useRules('softwareUpgradeRules');/* 校验规则 */
const disabledbtn = ref(false);/* 按钮可用与不可用 */
const formLoading = ref(false);/* 表单loading状态 */
let { total, pageSizes, multipleTableRef, tableLoading, tableState, searchCondition,
  handleCurrentChange, handleSizeChange, getTableInfo, searchInfo, resetSearch, batchDelete, deleteInfo, rowDblclick } =
  useTable({
    id: '',
    type: '',
    version_code: '',
    current: 1,
    size: 10
  }, 'getAppVersionInfo', disabledbtn);

let { formRef: versionFormRef, formData: versionForm, methodType, isDisabledbtn,
  closeOnClickModal, dialogTitle, editVisible, closedFormDialog,
  submitForm } = useDialogForm({
    id: '',
    type: '',
    version_code: '',
    descriptions: []
  }, disabledbtn, handleCustomOperation, formLoading);

const state = reactive({
  selectPlatformType: [],/* 平台类型 */
});
let { getDictDataInfo } = useDictData();

const id = ref(-1);/* 删除时所需的编号 删除时的id */

/* 格式化描述信息 */
const handleFormatterDescriptions = (row, column) => {
  return JSON.parse(row.descriptions)
    .map((item) => item.name)
    .join(';');
}
/* 格式化时间 */
const handleFormatterAddTime = (row, column) => {
  return Common.dateFormatYMDHMS(row.add_time * 1000);
}

/* 点击编辑按钮 */
const handleViewEdit = (row, code, name) => {
  if ('view' === code) {
    dialogTitle.value = name;
    isDisabledbtn.value = true;
    closeOnClickModal.value = true;
  } else if ('edit' === code) {
    dialogTitle.value = name;
    methodType.value = 'PUT';
  }
  nextTick(() => {
    versionForm.id = row.id;
    versionForm.type = row.type;
    versionForm.version_code = row.version_code;
    versionForm.descriptions = JSON.parse(row.descriptions);
  })
  editVisible.value = true;
}
/* 点击新增描述信息 */
const handleAddDesc = (params) => {
  versionForm.descriptions.push({
    id: new Date().getTime(),
    name: ''
  });
}
/* 删除描述信息 */
const handleDeleteDesc = (id) => {
  for (let i = 0; i < versionForm.descriptions.length; i++) {
    if (versionForm.descriptions[i].id === id) {
      versionForm.descriptions.splice(i, 1);
      break;
    }
  }
}

/* 保存与编辑 前的自定义校验或赋值操作 */
const handleValidAssignment = () => {
  for (const item of versionForm.descriptions) {
    if (item.name === '') {
      ElMessage.warning('请完整填写描述信息');
      return false;
    }
  }
  return true;
}

/*关闭模态框以后的自定义操作 */
function handleCustomOperation () {
  versionForm.id = '';/* 这个还需要手动清除 */
}

/* 获取表格信息 */
getTableInfo();
/* 初始化请求类型，平台类型信息 */
getDictDataInfo('APP_VERSION_TYPE', state, 'selectPlatformType');
</script>

<style scoped>
/* layout-布局 */
.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.textAlignCenter {
  text-align: center;
}

/* 修改select的盒模型 */
.el-select {
  display: block;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 修改该属性值  查询条件，查询结果得样式 */
.el-breadcrumb {
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 10px;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

/* 修改Switch开关 */
.el-switch {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.w-i-plus {
  margin-left: 5px;
  font-size: 30px;
  color: #409eff;
  cursor: pointer;
  transition: .5s;
}

.w-i-remove {
  margin-left: 5px;
  font-size: 30px;
  color: #f00;
  cursor: pointer;
  transition: .5s;
}

.w-i-plus:hover,
.w-i-remove:hover {
  opacity: 0.5;

}

/* .w-container + .w-container {
  margin-top: 10px;
} */
.w-container {
  width: 100%;
}

.w-content {
  display: flex;
}

.w-content + .w-content {
  margin-top: 10px;
}
</style>

/* scoped 不要添加 */
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  display: block;
  width: 178px;
  height: 178px;
  object-fit: cover;
}

.el-upload--text {
  width: auto;
}

/* 修改Switch开关 */
.el-switch {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* tag相关 start*/
.ibDraggable {
  display: inline-block;
  margin: 0 0 10px 10px;
}

/* .el-tag {
  margin: 0 0 10px 10px;
} */
.button-new-tag {
  margin-left: 10px;
  height: 28px;
  line-height: 28px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
}

/* end */

.el-upload__tip {
  font-size: 15px;
  font-weight: bold;
  color: #f00;
}
</style>
